<?php
/*
Plugin Name: Disable Comment RSS
Plugin URI: http://blog.fivestone.info/?p=664
Description: Disable RSS of posts or comments separately
Version: 0.8
Author: fivestone.info
Author URI: http://fivestone.info/

Copyright 2009 fivestone  (email : contact[at]fivestone.info)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
/*
add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);
*/

if (!class_exists("Fivestone_Disable_RSS")) {

	class Fivestone_Disable_RSS {

		var $adminOptionsName = "Fivestone_Disable_RSS";

		function Fivestone_Disable_RSS() { //constructor
			register_deactivation_hook( __FILE__, array(&$this, 'uninstall') );
			add_action('admin_menu', array(&$this, 'admin_menu'));
			//add_action('do_feed', 'fb_disable_feed', 1);
			add_action('do_feed_rdf', array(&$this, 'judge_rss'), 1);
			add_action('do_feed_rss', array(&$this, 'judge_rss'), 1);
			add_action('do_feed_rss2', array(&$this, 'judge_rss'), 1);
			add_action('do_feed_atom', array(&$this, 'judge_rss'), 1);
		}

		function init() {
			$this->getAdminOptions();
		}

		function admin_menu() {
			add_options_page('Disable Comment RSS', 'Disable Comment RSS', 9, basename(__FILE__), array(&$this, 'options_page'));
		}

		//Returns an array of admin options
		function getAdminOptions() {
			$fisAdminOptions = array('disable_rss_post' => '0', 'disable_rss_comment' => '0');

			$fisOptions = get_option($this->adminOptionsName);
			if (!empty($fisOptions)) {
				foreach ($fisOptions as $key => $option)
				$fisAdminOptions[$key] = $option;
			}
			//update_option($this->adminOptionsName, $fisAdminOptions);
			return $fisAdminOptions;

		}

		function options_page() {
			$fisOptions = $this->getAdminOptions();
			if (isset($_POST['update_fisOptions'])) {
				if ('1' == $_POST['fis_disable_rss_post']) {
					$fisOptions['disable_rss_post'] = '1';
				} else{
					$fisOptions['disable_rss_post'] = '0';
				}
				if ('1' == $_POST['fis_disable_rss_comment']) {
					$fisOptions['disable_rss_comment'] = '1';
				} else{
					$fisOptions['disable_rss_comment'] = '0';
				}
				update_option($this->adminOptionsName, $fisOptions);
?>
 <div class="updated"><p><strong><?php _e("Settings Updated.", "Fivestone_Disable_RSS");?></strong></p></div>
 <?php
	} ?>

<div class=wrap>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<h2>Disable Comment RSS Setting</h2>
<p><input type="checkbox" name="fis_disable_rss_post" value="1" <?php if ($fisOptions['disable_rss_post'] == "1") { echo 'checked="checked"'; }?>/> disable RSS for posts</p>
<p><input type="checkbox" name="fis_disable_rss_comment" value="1" <?php if ($fisOptions['disable_rss_comment'] == "1") { echo 'checked="checked"'; }?>/> disable RSS for comments</p>
<p><input type="submit" name="update_fisOptions" value="<?php _e('Update Settings', 'Fivestone_Disable_RSS') ?>" /></p>
</form>
</div>
<?php
		}//End function options_page()

		function judge_rss( $for_comments ) {
			$fisOptions = $this->getAdminOptions();
			if ( $for_comments )
			{
				if ( '1' == $fisOptions['disable_rss_comment'] )
				{
					disable_msg();
				}
			}else if( '1' == $fisOptions['disable_rss_post'] )
			{
				disable_msg();
			}
		}

		function disable_msg() {
			wp_die( __('The feed is disabled, please visit the <a href="'. get_bloginfo('url') .'">blog</a>.') );
		}

		function uninstall()
		{
			delete_option($this->adminOptionsName);
		}

	}
}

if (class_exists("Fivestone_Disable_RSS")) {
	$fis_DisRSS = new Fivestone_Disable_RSS();
}
?>